<?php
namespace app\admin\controller;
/**
 * #优惠卷管理#{用户优惠卷相关}
 */
class Coupon extends Common{
    function index(){
        $list=model($this->table)->readAll([],true);
        $this->assign('list',$list);
        return view();
    }
    function add($id=0){
        if(request()->isAjax()||request()->isPost()){
            $data=[
                'title'=>input('title/s',''),
                'usable'=>input('usable/f',0),
                'remark'=>input('remark/s',''),
                'value'=>input('value/f'),
                'maxs'=>input('maxs/d',0),
                'nums'=>input('nums/d',0),
                'expire'=>input('expire/d',0),
                'status'=>input('status/d',1),
                'type'=>input('type/d',0),
            ];
            $validate=$this->validate($data,[
                'title|优惠卷名称'=>'require',
                'usable|可用的临界金额'=>'require',
                'value|优惠卷价值'=>'require',
                'title|优惠卷名称'=>'require',
            ]);
            if(true!==$validate){
                $this->error($validate);
            }
            if($id>0){
                $has=model($this->table)->where(['id'=>$id])->count();
                if(!$has){
                    $this->error('该优惠卷不存在或已删除');
                }
                $data['id']=$id;
                $o=model($this->table)->edit($data);
            }else{
                $data['now_time']=strtotime(date('Ymd'));
                $o=model($this->table)->add($data);
            }
            if($o){
                $this->success(lang('Successful operation'));
                die;
            }
            $this->error(lang('Failed operation'));
        }
    }
    function close($id=0){
        if(request()->isAjax()||request()->isPost()){
            $ids=input('ids/a',[]); // 多项
            $rs=$this->_close('coupon',$id,$ids);
            if($rs['success']){
                $this->success($rs['msg']);
                die;
            }
            $this->error($rs['msg']);
        }
    }
    
}